﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VPLink2014.Data.MesApplis
{
	public static class VPLinkLogCatalogue
	{
		public static List<VOApplication> GetAllOrphanApplications()
		{
			List<VOApplication> orphanApplis = new List<VOApplication>();
			using( VPLinkLogConnection db = new VPLinkLogConnection() )
			{
				var orphanDbApplis = db.VplinkEntities.Applications.Where(
					appli => !db.VplinkEntities.AppliDepts.Any( dept => dept.appli == appli.id )
					&& !db.VplinkEntities.AppliServices.Any( sce => sce.appli == appli.id )
					&& !db.VplinkEntities.AppliDirections.Any( dir => dir.appli == appli.id ) );

				if( orphanDbApplis != null )
				{
					orphanDbApplis.ToList<Application>().ForEach( x => orphanApplis.Add( VOApplicationMapper.ConvertToApplication( x ) ) );
				}
			}
			orphanApplis.Sort( ( x, y ) => CompareVOApplication( x, y ) ); ;
			return orphanApplis;
		}
		public static List<VODirection> getDirections()
		{
			List<VODirection> directions = new List<VODirection>();
			using( VPLinkLogConnection db = new VPLinkLogConnection() )
			{
				directions = ( from dir in db.VplinkEntities.Directions
						 select new VODirection() { Name = dir.nom, Id = dir.id } ).ToList<VODirection>();
			}
			directions.Sort( ( a, b ) => string.Compare( a.Name, b.Name ) );
			return directions;
		}
		public static List<VODepartement> getDepartements()
		{
			List<VODepartement> departements = new List<VODepartement>();
			using( VPLinkLogConnection db = new VPLinkLogConnection() )
			{
				departements = ( from dir in db.VplinkEntities.Departements
						 select new VODepartement() { Name = dir.nom, Id = dir.id } )
						 .ToList<VODepartement>();
			}
			departements.Sort( ( a, b ) => string.Compare( a.Name, b.Name ) );
			return departements;
		}
		public static List<VOService> getServices()
		{
			List<VOService> services = new List<VOService>();
			using( VPLinkLogConnection db = new VPLinkLogConnection() )
			{
				services = ( from dir in db.VplinkEntities.Services
						 select new VOService() { Name = dir.nom, Id = dir.id } ).ToList<VOService>();
			}
			services.Sort( ( a, b ) => string.Compare( a.Name, b.Name ) );
			return services;
		}
		private static void AddApplications( AppliService appliSce, List<VOApplication> applis )
		{
			if( applis.Where( x => x.Id == appliSce.appli ).Count() == 0 )
			{
				applis.Add( new VOApplication()
				{
					Id = appliSce.appli,
					Name = appliSce.Application.titre,
					Description = appliSce.Application.description,
					Categorie = string.IsNullOrEmpty( appliSce.Application.categorie ) ? "Autre" : appliSce.Application.categorie,
					Url = appliSce.Application.url,
					Rang = appliSce.rang
				} );
			}
		}
		private static void AddApplications( AppliDept appliDept, List<VOApplication> applis )
		{
            if( applis.Where( x => x.Id == appliDept.appli ).Count() == 0 )
			{
				applis.Add( new VOApplication()
				{
					Id = appliDept.appli,
					Name = appliDept.Application.titre,
					Description = appliDept.Application.description,
					Categorie = string.IsNullOrEmpty( appliDept.Application.categorie ) ? "Autre" : appliDept.Application.categorie,
					Url = appliDept.Application.url,
					Rang = appliDept.rang
				} );
			}
		}
		private static void AddApplications( Departement dept, List<VOApplication> applis )
		{
			dept.AppliDepts.ToList<AppliDept>().ForEach( appli => AddApplications( appli, applis ) );
		}

		private static void AddApplications( Service sce, List<VOApplication> applis )
		{
			sce.AppliServices.ToList<AppliService>().ForEach( appli => AddApplications( appli, applis ) );
		}
		public static List<VOApplication> GetDirectionRecursiveApplications( VODirectionMapper direction )
		{
			List<VOApplication> directionApplications = new List<VOApplication>();
			directionApplications = direction.GetApplicationsTeam();
			using( VPLinkLogConnection db = new VPLinkLogConnection() )
			{
				Direction directionDb = ( from Direction dir in db.VplinkEntities.Directions
										   where dir.id == direction.direction.Id
										   select dir ).FirstOrDefault<Direction>();
				
                directionDb.Departements.ToList<Departement>()
					.ForEach( dept => AddApplications( dept, directionApplications ) );

				directionDb.Departements.ToList<Departement>()
					.ForEach( dept => dept.Services.ToList<Service>()
						.ForEach( sce => AddApplications( sce, directionApplications ) ) );
			}
			directionApplications.Sort( ( x, y ) => CompareVOApplication(x,y) );
			return directionApplications;
		}

		private static int CompareVOApplication( VOApplication x, VOApplication y )
		{
			if( string.Compare( x.Categorie, y.Categorie ) == 0 )
			{
				return string.Compare( x.Name, y.Name );
			}
			else
			{
				return string.Compare( x.Categorie, y.Categorie );
			}
		}
	}
}
